<extend name="./base" />
<block name="pageContent">
    <link rel="stylesheet" type="text/css" href="/Public/AdminTpl/webUploader/css/webuploader.css" />
    <link rel="stylesheet" type="text/css" href="/Public/AdminTpl/webUploader/css/style.css" />
    <div class="main-content">
        <!-- 头部面包屑导航条 -->
        <div class="breadcrumbs" id="breadcrumbs">
            <!--导航路径列表-->
            <ul class="breadcrumb no-margin-left">
                <li>
                    <a>管理后台</a>
                </li>
                <li class="action">
                    我的学堂
                </li>
            </ul>
        </div>
        <!--页面容器-->
        <div class="page-content">
            <div class="nav-tab" style="position: relative">
                <ul class="nav-tab-title ">
                    <li class="nav-this">添加课程</li>
                </ul>
            </div>
            <div class="row">
                <div class="col-xs-12">
                    <div class="row">
                        <form class="jsform" method="post" onsubmit="return subForm()">
                            <div class="s_form-textarea">
                                <span>选择分类 </span>
                                <select class="form-control s_form-width" id="onetype" datatype="*" onchange="getTwoType(this)">
                                    <option value="0">请选择</option>
                                    <foreach  name="allType" item="vo" key="k">
                                        <option value="{$k}">{$vo}</option>
                                    </foreach>
                                </select>
                                <select class="form-control s_form-width" id="twotype" datatype="*">
                                    <option value="0">请选择</option>
                                </select>
                            </div>
                            <div class="s_form-textarea s_share-textarea">
                                <span>课程标题</span>
                                <input placeholder="请输入课程标题" id="title" datatype="*" class="form-control s_form-width">
                            </div>
                            <div class="s_form-textarea s_share-textarea">
                                <span>排序</span>
                                <input id="sort" placeholder="不填默认为0,数字越大越靠前" class="form-control s_form-width">
                            </div>
                            <div class="s_form-textarea s_share-textarea">
                                <span>封面图片</span>
                                <ul class="ace-thumbnails clearfix showUpImg">
                                    <li style="background: #f5f6f8;" class="uploadImg">
                                        <div class="text-center border-hui" id="upimg" style="width: 100px;height: 90px;">
                                            <img src="{$Think.config.DOMAIN_URL}/Public/AdminTpl/img/edit_add_icon.png">
                                            <div class="space-4"></div>
                                        </div>
                                    </li>
                                </ul>
                            </div>
                            <div class="s_form-textarea s_share-textarea">
                                <span>课程简介</span>
                                <textarea id="info" style="width:800px;" datatype="*" placeholder="请输入新闻简介" class="form-control"></textarea>
                            </div>
                            <div class="s_form-textarea s_share-textarea">
                                <span>内容</span>
                                <div id="editor1"  style="width:800px;height: 500px" name="info">
                                </div>
                            </div>
                            <div style="margin: 0px 80px;">
                                <button class="btn" type="submit" style="width: 100px; border-radius: 2rem; background: #0451d7!important;border:1px solid #0451d7;margin-left: 20px;">保存</button>
                                <button class="btn" type="button" onclick="locationTo('{$Think.config.DOMAIN_URL}/course/index')" style="width: 100px; border-radius: 2rem; border:1px solid #999999;margin-left: 20px;color:#999 !important;background: #fff">取消</button>
                            </div>
                        </form>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <script type="text/javascript" charset="utf-8" src="/Public/AdminTpl/wangeditor/wangEditor.min.js"></script>
    <script type="text/javascript" src="/Public/AdminTpl/webUploader/dist/webuploader.js"></script>
    <script type="text/javascript" src="/Public/AdminTpl/webUploader/dist/mupload.js"></script>
    <script>
        var E = window.wangEditor;
        var editor = new E('#editor1');
        editor.customConfig.uploadImgServer = '{$Think.config.FILE_UPLOAD_SERVE_WANGEDIT}';
        editor.create();

        //webupload
        $(document).ready(function () {
            createUpload('upimg');
            $('.uploadImg').click(function () {
                thisContainer = $(this);
            });
        });
        function createUpload(upid) {
            uploader.addButton({
                id: '#'+upid,
                innerHTML: ''
            });
        }

        $(".jsform").Validform();
        //提交表单
        function subForm() {
            var croveImg = $('.showUpImg').find('.logoarr').val();
            if(!croveImg){
                layer.msg('请上传封面图片', {icon: 5,time: 1500});
                return false;
            }
            if(!editor.txt.text()){
                layer.msg('请填写新闻内容', {icon: 5,time: 1500});
                return false;
            }
            if(!$('#onetype').val() || !$('#twotype').val() || !$('#title').val() || !$('#info').val()){
                return false;
            }
            $.ajax({
                type:"post",
                dataType:"json",
                async:false,//同步方式
                url:"/course/addCourse",
                data:{
                    onetype: $('#onetype').val(),
                    twotype: $('#twotype').val(),
                    title: $('#title').val(),
                    info: $('#info').val(),
                    content: editor.txt.html(),
                    cover_img: croveImg,
                    sort: $('#sort').val() ? $('#sort').val() : 0
                },
                success:function(data){
                    if(data.code == 0){
                        layer.msg('操作成功', {icon: 6,time: 1500});
                        location.href = '/course/index';
                    }else{
                        layer.msg(data.msg, {icon: 5,time: 1500});
                        return false;
                    }
                }
            });
            return false;
        }
        //选择二级分类
        function getTwoType(that) {
            var oneType = $(that).val();
            $.ajax({
                type:"post",
                dataType:"json",
                url:"/tools/courseType",
                data:{
                    pid: oneType
                },
                success:function(data){
                    var html = '<option value="0">请选择</option>';
                    for(key in data){
                        html += '<option value="'+key+'">'+data[key]+'</option>';
                    }
                    $('#twotype').html(html);
                }
            });
        }
    </script>
</block>